﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : %{Cpp:License:FileName} --- %{Cpp:License:ClassName}
 *Auth       : Niyh  (QQ:393320854)
 *Create    : 2021-9-28
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2021/9/28/9/28
 *******************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QZXing/QZXing.h"

#include <QPainter>
#include <QDebug>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actionexport_triggered()
{
    QString strImage = QFileDialog::getOpenFileName(this, "选择图片", "", "*.png;*.jpg");
    if (strImage.isEmpty()) return;

    QImage image(strImage);
    QZXing decoder;
    decoder.setDecoder( QZXing::DecoderFormat_None);

    QList<s_codeinfo_t> results = decoder.decodeMultiCodes(image);
    if (results.isEmpty()) {
        ui->textBrowser->setText("未识别到有效二维码");
        return;
    }

    QPixmap pixmap(strImage);
    QPainter painter(&pixmap);
    painter.setPen(QPen(Qt::white, image.width() * 0.01));
    painter.setBrush(QColor("#0bb25e"));
    QFont font("Microsoft YaHei");
    font.setPixelSize(image.width() * 0.1);
    font.setBold(true);
    painter.setFont(font);
    ui->textBrowser->setText(QString("总共识别： %1 个二维码").arg(results.size()));
    qDebug() << "over" << QString("总共识别： %1 个二维码").arg(results.size());
    for (int i = 0; i < results.size(); i++) {
        s_codeinfo_t codec = results.at(i);
        ui->textBrowser->append(QString("    %1 %2").arg(i + 1).arg(codec.strText));
        int nW = qMin(codec.rect.width(), codec.rect.height()) * 0.25;
        painter.drawEllipse(codec.rect.center(), nW, nW);
        painter.drawText(codec.rect, Qt::AlignCenter, QString::number(i + 1));
    }

    ui->labelPixmap->setPixmap(pixmap.scaled(ui->labelPixmap->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
